//
//  YYSuggestViewController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/21.
//  Copyright © 2017年 stoneobs. All rights reserved.
////**********************意见反馈***************************

import UIKit

class YYSuggestViewController: STTableViewController {

    var textView:STTextView?
    var finshButtton:STButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "意见反馈";
        
        weak var weakSelf = self;
        NotificationCenter.default.addObserver(self, selector: #selector(didReciveNotifacationAction), name: NSNotification.Name.UITextViewTextDidChange, object: nil);
        textView = STTextView.init(frame: CGRect.init(x: 0, y: 25, width: SCREEN_WIDTH, height: 200));
        textView?.backgroundColor = .white;
        textView?.placeholder = "此时朕正在lol，有啥事就说吧";
    
        
        let headerView = UIView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: 300));
        headerView.backgroundColor = self.tableView.backgroundColor;
        headerView.addSubview(textView!);
        
        finshButtton =  STButton.init(frame: CGRect.init(x: 40, y: (textView?.bottom)! + 20, width: SCREEN_WIDTH - 80 , height: 40),
                                                   title: "提交",
                                                   titleColor: .white,
                                                   titleFont: 16,
                                                   cornerRadius: 5,
                                                   backgroundColor: NO_USER_INTERFACE_COLOR,
                                                   backgroundImage: nil,
                                                   image: nil);
        finshButtton?.contentHorizontalAlignment = .center;
        finshButtton?.clicAction = { but in
        
            weakSelf?.loadSuggestNetRequest();
        }
        headerView.addSubview(finshButtton!);
        self.tableView.tableHeaderView = headerView;
        
        
        let test = BmobQuery.init(className: suggestTableName);
        test?.findObjectsInBackground({ (array, error) in
            
        })
        
    }

    func didReciveNotifacationAction(notify:NSNotification) -> Void {
        
        if (self.textView?.text?.characters.count)! >= 5{
            
            self.finshButtton?.isUserInteractionEnabled = true;
            self.finshButtton?.backgroundColor = UIColor.flatMint;
        }else{
            
            self.finshButtton?.isUserInteractionEnabled = false;
            self.finshButtton?.backgroundColor = NO_USER_INTERFACE_COLOR;
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
        // MARK:Network Request
    func loadSuggestNetRequest() -> Void {
        
        SVProgressHUD.show(withStatus: "你的奏折正在上传，请稍后");
        self.finshButtton?.isUserInteractionEnabled = false;
        let suggestObject:BmobObject = BmobObject(className: suggestTableName);
        suggestObject.setObject((self.textView?.text)!, forKey: "content");
        
        suggestObject.setObject(BmobUser.init(outDataWithClassName: userTableName,
                                              objectId: YYUserLoginedDefultManger.shared().currentUser().objectId),
                                forKey: "comeFrom");
   
        suggestObject.saveInBackground { (success, error) in
            SVProgressHUD.dismiss();
            self.finshButtton?.isUserInteractionEnabled = true;
            if success == true{
                SVProgressHUD.showSuccess(withStatus: "爱卿的宝贵建议，朕已阅");
                _ = self.navigationController?.popViewController(animated: true);
            }else{
                SVProgressHUD.showError(withStatus: error?.localizedDescription);
            }
        }
    }
    


}
